package edu.bupt.e1btree;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

public class T01BinaryTreePreorder {
    public List<Integer> preorderTraversal(TreeNode root) {
        ArrayList<Integer> integers = new ArrayList<Integer>();
        if (null == root) return integers;
        Stack<TreeNode> cache = new Stack<TreeNode>();
        cache.push(root);
        while (!cache.empty()){
            TreeNode pop = cache.pop();
            integers.add(pop.val);
            if (null != pop.right)
                cache.push(pop.right);
            if (null != pop.left)
                cache.push(pop.left);
        }
        return integers;
    }


}
